package view
{
	import events.ReaderEvent;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.display.StageDisplayState;
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.external.ExternalInterface;
	import flash.geom.Rectangle;
	import flash.printing.PrintJob;
	import flash.printing.PrintJobOptions;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
	import flash.utils.setTimeout;
	
	import model.ReaderModel;
	
	import view.ui.PageCont;
	
	public class PrintPopup extends Sprite
	{
		private var _stage:Stage;
		
		private var _print_skin:SKprint;
		
		private var _print_loading:SKprinting_loading;
		
		private var _m:ReaderModel = ReaderModel.getInstance();
		
		private var _selectedOption:int = -1;
		
		private var _printsList:Array = [];
		private var _pageReadyNum:int;
		private var _pagePrintTotal:int;
		
//		private var _captureBmpd:BitmapData;
//		private var _capture:Bitmap;
		private var _block:Sprite;
		
		private var _t_f:TextField;
		private var _t_to:TextField;
		
		private var _v1:Boolean = false;
		private var _v2:Boolean = false;
		
		public function PrintPopup()
		{
			init();
		}
		
		private function init():void
		{
//			_captureBmpd = new BitmapData(100, 100);
//			_capture = new Bitmap(_captureBmpd);
//			addChild(_capture);
			
			_block = new Sprite();
			addChild(_block);
			
			_print_skin = new SKprint();
			
			_print_skin.btnClose.addEventListener(MouseEvent.CLICK, onBtnCloseClick);
			_print_skin.btnCancel.addEventListener(MouseEvent.CLICK, onBtnCloseClick);
			_print_skin.r1.addEventListener(MouseEvent.CLICK, onRadioClick);
			_print_skin.r2.addEventListener(MouseEvent.CLICK, onRadioClick);
			_print_skin.r3.addEventListener(MouseEvent.CLICK, onRadioClick);
			_print_skin.btnPrint.addEventListener(MouseEvent.CLICK, onBtnPrintClick);
			
			_t_f = new TextField();
			_t_f.type = TextFieldType.INPUT;
			_t_f.multiline = false;
//			_t_f.autoSize = TextFieldAutoSize.LEFT;
			_t_f.width = 62;
			_t_f.height = 25;
			_t_to = new TextField();
			_t_to.type = TextFieldType.INPUT;
			_t_to.multiline = false;
//			_t_to.autoSize = TextFieldAutoSize.LEFT;
			_t_to.width = 62;
			_t_to.height = 25;
			_t_f.x = -33;
			_t_f.y = 9;
			_print_skin.addChild(_t_f);
			
			_t_f.addEventListener(FocusEvent.FOCUS_IN, onTFromFocuseIn);
			_t_f.addEventListener(FocusEvent.FOCUS_OUT, onTFromFocuseOut);
			_t_to.addEventListener(FocusEvent.FOCUS_IN, onTToFocuseIn);
			_t_to.addEventListener(FocusEvent.FOCUS_OUT, onTToFocuseOut);
			
			
			_t_f.restrict = "0-9";
			_t_to.restrict = "0-9";
			
			_t_to.x = 73;
			_t_to.y = 9;
			_print_skin.addChild(_t_to);
			
			addChild(_print_skin);
			
			_print_loading = new SKprinting_loading();
			addChild(_print_loading);
		}
		
		protected function onTFromFocuseIn(e:FocusEvent):void
		{
			_t_f.addEventListener(Event.CHANGE, onTFChange);
		}
		
		protected function onTFChange(e:Event):void
		{
			if(_t_f.text == "") return;
			var tid:int = int(_t_f.text);
			if(tid < 1) tid = 1;
			if(tid > _m.total_page_num) tid = _m.total_page_num;
			_t_f.text = String(tid);
		}
		
		protected function onTFromFocuseOut(e:FocusEvent):void
		{
			_t_f.removeEventListener(Event.CHANGE, onTFChange);
			if(_t_f.text == "") _t_f.text = "1";
		}
		
		protected function onTToFocuseIn(e:FocusEvent):void
		{
			_t_to.addEventListener(Event.CHANGE, onTToChange);
		}
		
		protected function onTToChange(e:Event):void
		{
			if(_t_to.text == "") return;
			var tid:int = int(_t_to.text);
//			var ts:int = int(_t_f.text);
			if(tid < 1) tid = 1;
			if(tid > _m.total_page_num) tid = _m.total_page_num;
			_t_to.text = String(tid);
		}
		
		protected function onTToFocuseOut(e:FocusEvent):void
		{
			_t_to.removeEventListener(Event.CHANGE, onTToChange);
			var ts:int = int(_t_f.text);
			var tid:int = int(_t_to.text);
			if(tid < ts) tid = ts;
			_t_to.text = String(tid);
		}
		
		protected function onBtnPrintClick(e:MouseEvent):void
		{
			_print_loading.visible = true;
			_print_skin.visible = false;
			_v1 = false;
			_v2 = true;
			
			_printsList = [];
			_pageReadyNum = 0;
			_m.addEventListener(ReaderEvent.PAGE_LOADED, onPageReady);
			switch(_selectedOption)
			{
				case 1:
					_pagePrintTotal = _m.total_page_num;
					for(var i:int = 0, len:int = _m.total_page_num ; i < len ; i ++ )
					{
						var p:PageCont = _m.getPage(i + 1) as PageCont;
						_printsList[_printsList.length] = p;
						_m.loadPage(i + 1);						
					}
					break;
				case 2:
					_pagePrintTotal = 1;
					p = _m.getPage(_m.current_page) as PageCont;
					_printsList[_printsList.length] = p;
					_m.loadPage(_m.current_page);			
					break;
				case 3:
					var start_p:int = int(_t_f.text);		
					if(start_p < 1) start_p = 1;
					if(start_p > _m.total_page_num) start_p = _m.total_page_num;
					var end_p:int = int(_t_to.text);
					if(end_p > _m.total_page_num) end_p = _m.total_page_num;
					if(end_p < start_p) end_p = start_p;
					
					_pagePrintTotal = end_p - start_p + 1;
					
					for( i = 0 ; i < _pagePrintTotal ; i ++)
					{
						p = _m.getPage(i + start_p) as PageCont;
						_printsList[_printsList.length] = p;
						_m.loadPage(i + start_p);	
					}		
					break;
			}
			return;		
		}
		
		protected function onPageReady(e:ReaderEvent):void
		{
			_pageReadyNum += 1;
			if(_pageReadyNum == _pagePrintTotal)
			{
				_m.removeEventListener(ReaderEvent.PAGE_LOADED, onPageReady);
				var printJob:PrintJob = new PrintJob();
				if(printJob.start())
				{
					for(var i:int = 0, len:int = _printsList.length ; i < len; i ++)
					{
						var p:Sprite = _printsList[i] as Sprite;
						if (p.width>printJob.pageWidth) {
							p.width=printJob.pageWidth;
							p.scaleY = p.scaleX;
						}
						p.x = - p.width;
						_stage.addChild(p);
						printJob.addPage(p as Sprite, null, new PrintJobOptions(true));						
					}
					printJob.send();
					
					for( i = 0 ; i < len ; i ++ )
					{
						p = _printsList[i] as Sprite;
						p.scaleX = p.scaleY = 1;
						p.parent.removeChild(p);
					}
					_m.refresh();
					hide();
				}else{
					_m.removeEventListener(ReaderEvent.PAGE_LOADED, onPageReady);
					_print_loading.visible = false;
					_print_skin.visible = true;
					_v1 = true;
					_v2 = false;		
				}
			}
			
		}
		
		protected function onRadioClick(e:MouseEvent):void
		{
			selectedOption = int(String(e.currentTarget.name).substr(1,1));
		}
		
		protected function onBtnCloseClick(event:MouseEvent):void
		{
			hide();
		}	
		
		public function layout():void
		{
			if(this.parent == null) return;
			
			this.x = this.y = 0;
			_print_skin.x = Math.round(_stage.stageWidth * 0.5);
			_print_skin.y = Math.round(_stage.stageHeight * 0.5);
			
			_block.graphics.clear();
			_print_skin.visible = false;
			_print_loading.visible = false;
			
			_block.graphics.clear();
			_block.graphics.beginFill(0x000000, 0.7);
			_block.graphics.drawRect(0,0, _stage.stageWidth, _stage.stageHeight);
			_block.graphics.endFill();
						
			_print_loading.x = _print_skin.x;
			_print_loading.y = _print_skin.y;
			
			_print_skin.visible = _v1;
			_print_loading.visible = _v2;
			
			_print_skin.r3_d_mc.visible = _stage.displayState == StageDisplayState.NORMAL;
			_t_f.visible = _t_to.visible = _print_skin.r3.visible = _print_skin.r3_d_mc.visible;
		}
		
		public function show(s:Stage):void
		{
			_stage = s;
			_stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			this.x = this.y = 0;
			_print_skin.x = Math.round(_stage.stageWidth * 0.5);
			_print_skin.y = Math.round(_stage.stageHeight * 0.5);
			
			_block.graphics.clear();
			_block.graphics.beginFill(0x000000, 0.7);
			_block.graphics.drawRect(0,0, _stage.stageWidth, _stage.stageHeight);
			_block.graphics.endFill();
			
			selectedOption = 1;			
			_print_loading.x = _print_skin.x;
			_print_loading.y = _print_skin.y;
			_print_skin.visible = true;
			_t_f.text = "1";
			_t_to.text = String(_m.total_page_num);
			_print_loading.visible = false;
			_stage.addChild(this);
			this.visible = true;
			_v1 = true;
			_v2 = false;		
			layout();
		}		
		
		protected function onKeyDown(e:KeyboardEvent):void
		{
			if(e.keyCode == 27)
			{
				hide();
			}
		}
		
		public function hide():void
		{
			_stage.removeEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
//			_stage.removeChild(this);
			_block.graphics.clear();
//			_m.block = false;
			this.visible = false;
			setTimeout( function ():void{
				_m.block = false;				
			}, 100);
//			_stage = null;
		}

		public function get selectedOption():int { return _selectedOption; }
		public function set selectedOption(value:int):void
		{
			if(value == _selectedOption) return;
			if(_selectedOption > 0)
			{
				MovieClip(_print_skin["r" + _selectedOption]).gotoAndStop(1);
			}
			_selectedOption = value;
			MovieClip(_print_skin["r" + _selectedOption]).gotoAndStop(2);
		}

	}
}